﻿define(["jquery","bbcare","jqprint",
    "scprit/department/vm/assessmentScreening/bsid2.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"
],function ($,$bb,$jqprint,$ui,$servers) {
    var event = {};
    event.init = function(obj){
        var userId = obj.userId,type = obj.type,answerId = obj.answerId;
        var hisParams = $servers.hisParams(userId,type,'1','');
        var hisFn = function(data){
            for (var i = 0; i < data.rows.length; i++) {
                data.rows[i].testAge = $servers.getAge($servers.dateDiff(data.rows[i].birthday,data.rows[i].testTime));
            }
            $ui.historyInit({"id":"#bsid-history","data":data.rows});
            if(!answerId){
                answerId = $("#bsid-history>li").eq(0).attr("answerId");
                totalFn(userId,type,answerId);
            }else{
                totalFn(userId,type,answerId);
            }
            //历史记录列表点击事件：
            $("#bsid-history>li").click(function(){
                answerId = $(this).attr("answerId");
                totalFn(userId,type,answerId);
            });
            $("#modify").click(function(){
                if($(this).attr("type")=="modify"){
                    $(".guides").attr("contentEditable",true).addClass("edit");
                    if($("#guides").html() == "暂无指导意见"){
                        $("#guides").html("");
                    }
                    $(this).attr("type","save").html('<i class="fa fa-check-square" aria-hidden="true"></i>确认修改');
                }else if($(this).attr("type")=="save"){
                    $(".guides").removeAttr("contentEditable").removeClass("edit");
                    $(this).attr("type","modify").html('<i class="fa fa-pencil-square" aria-hidden="true"></i>修改建议');
                    var guides = $.trim($("#guides").html());
                    var remark = $("#remark").length > 0 ? $.trim($("#remark").html()) : "";
                    if(!guides){
                        $("#guides").html("暂无指导意见");
                        guides = "暂无指导意见";
                    }
                    modifyGuides(userId,answerId,guides,remark,type);
                }
            });
            $("#modifyRemark").click(function(){
                if($(this).attr("type")=="modify"){
                    $(".remark").attr("contentEditable",true).addClass("edit");
                    if($("#remark").html() == "暂无备注"){
                        $("#remark").html("");
                    }
                    $(this).attr("type","save").html('<i class="fa fa-check-square" aria-hidden="true"></i>确认修改');
                }else if($(this).attr("type")=="save"){
                    $(".remark").removeAttr("contentEditable").removeClass("edit");
                    $(this).attr("type","modify").html('<i class="fa fa-pencil-square" aria-hidden="true"></i>修改备注');
                    var guides = $.trim($("#guides").html());
                    var remark = $.trim($("#remark").html());
                    if(!remark){
                        $("#remarkremark").html("暂无备注");
                        remark = "暂无备注";
                    }
                    modifyGuides(userId,answerId,guides,remark,type);
                }
            });
        };
        $servers.history(hisParams,hisFn);
    };
    var totalFn = function(userId,type,answerId){
        checkedFn(answerId);
        getInfo(userId,answerId,type);
        getGuides(userId,type,answerId);
    };
    //获取评估记录详情：
    var getInfo = function(userId,evaId,type){
        var reportParams = $servers.reportParamsBsid2(userId,evaId,type);
        var reportFn = function(data){
            var result = JSON.parse(data.data.result);
            var answerResult = JSON.parse(data.data.answerResult);
            var resultTotal = $.extend({},data.data,result);
            var diff = $servers.dateDiff(resultTotal.birthday,resultTotal.testTime);
            resultTotal.monthAge = $servers.getMonth(diff,resultTotal.pregnancyWeek).monthAge;
            resultTotal.age = $servers.age(diff,resultTotal.pregnancyWeek);
            resultTotal.height = answerResult.height;
            for(var item in resultTotal){
                if(item == "MR" || item == "PR"){
                    for(var its in resultTotal[item]){
                        resultTotal[item+its] = resultTotal[item][its];
                    }
                }
            }
            $ui.resultInfoInit({"id":"#info","data":resultTotal});
            $ui.resultInfoInit({"id":"#print-info","data":resultTotal});
            $("#hos-info").html('<p>'+resultTotal.hospName+'&nbsp;'+resultTotal.deptName+'</p><p>评估医生：'+resultTotal.doctorName+'</p>');
        };
        $servers.report(reportParams,reportFn);
    };
    //获取指导意见
    var getGuides = function (userId,exam,evaId){
        var params = $servers.getGuidesParams(userId,exam,evaId);
        var guidesFn = function(data){
            if( data.data ){
                $("#guides,#print-guides").html(data.data.guide);
                if( $("#remark").length > 0 ) $("#remark").html(data.data.remark)
            }else{
                $("#guides,#print-guides").html("暂无指导意见");
                if( $("#remark").length > 0 ) $("#remark").html("暂无备注")
            }
        };
        $servers.getGuides(params,guidesFn);
    };
    //更改指导意见：
    var modifyGuides = function (userId,evaId,guides,remark,exam){
        var params1 = $servers.modifyGuidesParams(userId,evaId,guides,remark);
        var guidesFn = function(data2){
            getGuides(userId,exam,evaId);
        };
        $servers.modifyGuides(params1,guidesFn);
    };
    var checkedFn = function(answerId){
        $("#bsid-history").find("li").each(function(i){
            var $this=$(this);
            if($this.attr("answerId")==answerId){
                $(this).addClass("select").siblings().removeClass("select");
            }
        });
    };
    return event;
});